package com.mzmarket.market.module.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * 数据库加密
 */
public class druidEncryptUtil {

    private static String publicKey; //公钥

    private static String privateKey; //私钥

    static {
        try {
            String[] keyPair = ConfigTools.genKeyPair(1024);
            privateKey = keyPair[0];
            System.out.println("privateKey:" + privateKey);
            publicKey = keyPair[1];
            System.out.println("publicKey:" + publicKey);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        }
    }



    //加密 -- 返回加密后密码
    public static String encrypt(String str) throws Exception {
        String encrypt = ConfigTools.encrypt(privateKey,str);
        System.out.println("encrypt : " + encrypt);
        return encrypt;
    }

    //解密
    public static String decrypt(String str) throws Exception {
        String decrypt = ConfigTools.decrypt(publicKey,str);
        System.out.println("decrypt : " + decrypt);
        return decrypt;
    }

    /**
     * 在启动时传递参数
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        //encrypt(args[0]);
        encrypt("mazheng1997");
    }

}
